<template>
    <button @click="start">开始</button>
    <button @click="panti">登录</button>
</template>

<script setup lang="ts">
import { io, Socket } from 'socket.io-client';
import { random, sample } from 'underscore';
import { onMounted, ref } from 'vue';
import { dealy, delay } from '../util/time';
import { userid, contexts, questionid, activityid } from './test'
const { origin } = location

interface TTT {
    time: number,
    userid: number,
    questionid: number,
    context: string
}

const Sockets = ref<Socket[]>([])

function lanjie() {
    for (let v of userid) {
        const t = io(origin + '/').on('test/savetmpmatchuserdata', (data) => {
            console.log(data)
        }).on('error', (e) => {
            console.log(e)
        })
        Sockets.value.push(t)
    }
}



async function panti() {
    let k = 0
    for (const v of userid) {

        const key = random(5)
        await delay(key * 1000)
        console.log(v)
        Sockets.value[k].emit('activity', {
            activityid,
            com: true,
            userid: v
        })
        k++
    }
    console.log('全部登录')

    for (let v = 0; v < 1000; v++) {
        console.log(v)
        const key = random(5)
        await dealy(key * 1000)
        const userid1 = sample(userid)
        Sockets.value[key].emit('atactivity', {
            activityid,
            userid: userid1
        })
    }


}


// 保存题目
function start() {
    let t: TTT[][] = []
    // 创建访问
    for (let v of userid) {
        const c: TTT[] = []
        for (let i of questionid) {
            const time = random(80)
            let context = ''
            if ([110748, 110747, 110746, 110745, 110744, 110744, 110742, 110741].includes(i)) {
                context = 'A'
            } else {
                context = sample(contexts) as string
            }
            c.push({
                time,
                userid: v,
                questionid: i,
                context
            })
        }
        t.push(c)
    }
    // 开始访问
    t.forEach(async (e, k) => {
        let key = 1
        for (const iterator of e) {
            const { userid, time } = iterator
            const data: Record<any, any> = {}
            e.slice(0, key).forEach(e => {
                data[e.questionid] = {
                    context: e.context,
                    language: 'CPP'
                }
            })
            await delay(time * 1000)
            Sockets.value[k].emit('test/savetmpmatchuserdata', {
                activityid,
                userid,
                data,
                seconds: 1000 * 60 * 60 * 24
            })
            key++
        }

    })

}


onMounted(() => {
    lanjie()
})

</script>

